<?php
/**
 * 配送方式模型
 * @author cjli
 *
 */
class ShippingModel extends Model
{
	/**
	 * 更新配送方式信息
	 * @param array $post post 数据
	 * @param int $shipping_id 配送ID
	 */
	public static function updateShipping($post, $shipping_id = null)
	{
		$shipping_db = D('shipping');
		$data = array(
			'name' => isset($post['name']) ? strtolower(trim($post['name'])) : null,
			'type' => isset($post['type']) ? intval($post['type']) : 0,
			'first_weight' => intval($post['first_weight']),
			'second_weight' => intval($post['second_weight']),
			'first_price' => floatval($post['first_price']),
			'second_price' => floatval($post['second_price']),
			'status' => intval($post['status']),
			'listorder' => intval($post['listorder']),
			'is_save_price' =>intval($post['is_save_price']),
			'save_rate' =>intval($post['save_rate']),
			'low_price' =>floatval($post['low_price']),
			'price_type' =>intval($post['price_type']),
			'open_default' =>intval($post['open_default']),
			'free_price' => floatval($post['free_price']),
			'area' =>serialize(array_diff($post['area'], array(null,'null','',' '))),
			'area_groupid' => serialize(array_diff($post['area_groupid'], array(null,'null','',' '))),
			'firstprice' => serialize(array_diff($post['firstprice'], array(null,'null','',' '))),
			'secondprice' => serialize(array_diff($post['secondprice'], array(null,'null','',' '))),
			'freeprice' => serialize(array_diff($post['freeprice'], array(null,'null','',' '))),
			'freenum' => serialize(array_diff($post['freenum'], array(null,'null','',' '))),
		);
		if($shipping_id){
			$shipping_db->where('id='.$shipping_id)->save($data);
		}else{
			$shipping_id = $shipping_db->add($data);
		}
		
		if($shipping_id){
			$shipping_items_db = D('shipping_items');
			$shipping_items_db->where(array('shipping_id' => $shipping_id))->delete();
			$languageList = SiteModel::getLanguageList();
			foreach($languageList as $language_id => $language_name){
				if($post['shipping'][$language_id]['title']){
					$data = array(
						'shipping_id' => $shipping_id,
						'language_id' => $language_id,
						'title' => trim($post['shipping'][$language_id]['title']),
						'description' => trim($post['shipping'][$language_id]['description']),
                        'day_or_time' => trim($post['shipping'][$language_id]['day_or_time']),
					);
					$shipping_items_db->add($data);
				}
			}
		}
		return $shipping_id;
	}
	
	/**
	 * 获取配送方式列表
	 */
	public static function getShippingList()
	{
		$where = array(
			'status' => 1,
			'language_id' => C('language_id'),
		);
		$shipping_db = D('shipping');
		$shipping_db->field('shipping.id,shipping.name,shipping_items.title,shipping.free_price');
		$shipping_db->join('shipping_items ON shipping_items.shipping_id = shipping.id');
		$shippingList = $shipping_db->where($where)->order('listorder DESC, id ASC')->select();
		return $shippingList;
	}
	/**
	 * 获取配送方式信息
	 * @param int $shipping_id 配送方式ID
	 */
	public static function getShippingInfo( $shipping_id )
	{
		$shipping_db = D('shipping');
		$shipping = $shipping_db->find($shipping_id);
		if($shipping){
			$shipping['area'] = unserialize($shipping['area']);
			$shipping['area_groupid'] = unserialize($shipping['area_groupid']);
			$shipping['firstprice'] = unserialize($shipping['firstprice']);
			$shipping['secondprice'] = unserialize($shipping['secondprice']);
			$shipping['freeprice'] = unserialize($shipping['freeprice']);
			$shipping['freenum'] = unserialize($shipping['freenum']);
		}
		return $shipping ? $shipping : false;
	}
	
	/**
	 * 获取配送方式详细内容
	 * @param int $shipping_id 配送方式ID
	 * @param int $language_id	语言ID
	 */
	public static function getShippingItems($shipping_id, $language_id = null)
	{
		$where['shipping_id'] = $shipping_id;
		if($language_id){
			$where['language_id'] = $language_id;
		}
		$shippingArray = array();
		$shipping_items_db = D('shipping_items');
		$items = $shipping_items_db->where($where)->select();
		if($items){
			foreach ($items as $item){
				$shippingArray[$item['language_id']] = $item;
			}
		}
		return $shippingArray;
	}
	
	/**
	 * 获取配送方式信息
	 * @param int $name 配送方式名称
	 */
	public static function getShippingInfoByName($name)
	{
		$name = strtolower(trim($name));
		$shipping_db = D('shipping');
		$shipping = $shipping_db->where(array('name' => $name))->find();
		if($shipping){
			$shipping['area'] = unserialize($shipping['area']);
			$shipping['area_groupid'] = unserialize($shipping['area_groupid']);
			$shipping['firstprice'] = unserialize($shipping['firstprice']);
			$shipping['secondprice'] = unserialize($shipping['secondprice']);
			$shipping['freenum'] = unserialize($shipping['freenum']);
		}
		return $shipping ? $shipping : false;
	}
	
	/**
	 * 获取配送费用
	 * @param string $shipping 配送方式ID或者名称
	 * @param int $area_id 地区ID
	 * @param int $goods_amount 商品总额
	 * @param int $weight 商品重量
	 * @param int $goods_number 商品件数
	 * @return float
	 */
	public static function getShippingFee($shipping, $area_id = 0, $goods_amount = 0, $weight = 0, $goods_number=0)
	{
		$fee = 0;
		$is_true = 0;	//是否要计算费用
		
		
//		$shipping_id = intval($shipping_id);
		$area_id = intval($area_id);
//		$total_fee = floatval($total_fee);
		$weight = intval($weight);
		
		//获取配送方式信息
		if(is_numeric($shipping)){
			$shippingInfo = self::getShippingInfo($shipping);
		}else{
			$shippingInfo = self::getShippingInfoByName($shipping);
		}
		if(!$shippingInfo || $shippingInfo['status'] == 0){
			return $fee;
		}
		
		$first_weight = $shippingInfo['first_weight'];	//首重
		$second_weight = $shippingInfo['second_weight'];//续重
		$first_price = $shippingInfo['first_price']; 		//首重费用
		$second_price = $shippingInfo['second_price'];	// 续重费用 
		//统一设置运费
		if($shippingInfo['price_type'] == 0){
			if($shippingInfo['free_price'] > 0 && $goods_amount > $shippingInfo['free_price']){
				return $fee;
			}
			$is_true = 1;
		}
		//指定配送地区和费用
		else
		{
			$area_groupid = $shippingInfo['area_groupid'];
			$firstprice = $shippingInfo['firstprice'];
			$secondprice = $shippingInfo['secondprice'];
			$freeprice = unserialize($shippingInfo['freeprice']);
			$freenum = $shippingInfo['freenum'];
			if($area_id){
				//查看是否指定了该配送地区
				if(is_array($area_groupid) && $area_groupid)
				{
					foreach($area_groupid as $area_key => $area_ids)
					{
						if(empty($area_ids)) continue;
						$area_ids = explode(';', $area_ids);
						//指定了配送地区
						if(in_array($area_id, $area_ids))
						{
							$first_price = isset($firstprice[$area_key]) ? $firstprice[$area_key] : 0;
							$second_price = isset($secondprice[$area_key]) ? $secondprice[$area_key] : 0;
							$free_price = isset($freeprice[$area_key]) ? $freeprice[$area_key] : 0;
							$free_num = isset($freenum[$area_key]) ? $freenum[$area_key] : 0;
							
							if($free_price > 0 && $goods_amount > $free_price){
								return $fee;
							}
							
							$is_true = 1;
							break;
						}
					}
				}
			}
			
			 //没有指定配送地区但启用了默认费用
			if($is_true==0 && $shippingInfo['open_default'] == 1)
			{
				$is_true = 1;
			}else{
				// TODO 不能配送地区处理
			}
		}

		//要计算配送费用
		if($is_true)
		{	
			if($area_id){
				//指定配送地区和费用
				if($shippingInfo['price_type']==1){
				
					if($free_num >0 ){
						if($goods_number){
							$fee = $first_price*$goods_number;
						}
						
					}else{
						//商品重量小于首重量
						if($weight <= $first_weight){
							$fee = $first_price;
						}else{
							$differ_weight = $weight-$first_weight; //商品重量减去首重量的差值
							$fee = $first_price + ceil($differ_weight/$second_weight) * $second_price;
						}
					}
					
				}else{
					//商品重量小于首重量
					if($weight <= $first_weight){
						$fee = $first_price;
					}else{
						$differ_weight = $weight-$first_weight; //商品重量减去首重量的差值
						$fee = $first_price + ceil($differ_weight/$second_weight) * $second_price;
					}
				}
			}else{
				$fee = 0;
			}
			//按件数收费
			/*elseif($shippingInfo['price_type']==2){
				if($goods_number){
					$fee = $first_price * $goods_number;
				}
			}*/
		}else{
			return 'nono';
		}
		return number_format($fee, 2, '.', '');
	}
}